﻿using System;
using System.Web;
using System.Web.UI;

namespace TaskWebApplicationUI
{
    public class CookieHelper
    {
        private readonly Page _page;
        private HttpCookie aCookie;

        public CookieHelper(Page page)
        {
            _page = page;
        }

        public string ReadCookie(string cookieName)
        {
            return (_page.Request.Cookies[cookieName] != null)
                       ? _page.Server.HtmlEncode(_page.Request.Cookies[cookieName].Value).Trim()
                       : string.Empty;
        }

        public void WriteCookie(string cookieName, string cookieValue)
        {
            aCookie = new HttpCookie(cookieName) {Value = cookieValue, Expires = DateTime.Now.AddDays(365)};
            _page.Response.Cookies.Add(aCookie);
        }

        public void DeleteCookie(string cookieName)
        {
            aCookie = new HttpCookie(cookieName) {Expires = DateTime.Now.AddDays(-1)};
            _page.Response.Cookies.Add(aCookie);
        }
    }
}